﻿using CommandLine;
using CommandLine.Text;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace bcpJson
{
	public class ExportSetting
	{
		[Option('d', Required = false, HelpText = "Specifies the database of SQL Server which read from.")]
		public string Database { get; set; }
		[Option('q', Required = false, HelpText = "a Table name or a Transact-SQL query/procedure. If the query returns multiple result sets, only the first result set is copied to the data file; subsequent result sets are ignored.")]
		public string Query { get; set; }
		[Option('o', Required = true, HelpText = "out copies from the database table or view to a file.")]
		public string Path { get; set; }
		[Option('k', Required = false, HelpText = "Specifies that empty columns should retain a null value during the operation, rather than omit the columns inserted. ")]
		public bool ExportNull { get; set; }
		[Option('s', Required = false, DefaultValue="localhost", HelpText = "Specifies the instance of SQL Server to which to connect.")]
		public string Server { get; set; }
		[Option('u', Required = false, HelpText = "Specifies the login ID used to connect to SQL Server.")]
		public string UserName { get; set; }
		[Option('p', Required = false, HelpText = "Specifies the password for the login ID. ")]
		public string Password { get; set; }
		[Option('t', Required = false, HelpText = "Specifies that the tool connects to SQL Server with a trusted connection using integrated security.")]
		public bool IntegratedSecurity { get; set; }
		[Option('m', "mongodb", Required = false, HelpText = "Specifies whether generate import script for MongoDB")]
		public bool ImportScript { get; set; }

		public bool? MultiExport 
		{
			get
			{
				if (string.IsNullOrEmpty(this.Query) && !string.IsNullOrEmpty(this.Database))
				{
					return true;
				}
				else if (!string.IsNullOrEmpty(this.Query) && string.IsNullOrEmpty(this.Database))
				{
					return false;
				}
				else
				{
					return null;
				}
			}
		}

		[ParserState]
		public IParserState LastParserState { get; set; }

		public string GetUsage()
		{
			return HelpText.AutoBuild(this, (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
		}

		public string GetConnectionString()
		{
			if (this.IntegratedSecurity)
			{
				return string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;", this.Server, string.IsNullOrEmpty(this.Database) ? "master" : this.Database);
			}
			else
			{
				return string.Format("Data Source={0};User ID={1};Password={2};Initial Catalog={3};", this.Server, this.UserName, this.Password, string.IsNullOrEmpty(this.Database) ? "master" : this.Database);
			}
		}

		public bool Valid()
		{
			if (string.IsNullOrEmpty(this.Query) && string.IsNullOrEmpty(this.Database))
			{
				return false;
			}

			//TODO: PATH

			using (var conn = new SqlConnection(this.GetConnectionString()))
			{
				try
				{
					conn.Open();
					conn.Close();
				}
				catch
				{
					return false;
				}
			}

			return true;
		}
	}
}
